#include "terrianGLSL.h"
#include "GL/glut.h"

#include <cmath>
#include <iostream>
#include <strstream>

using namespace std;

#include "demDataLoad.h"

void drawModel_teapot()
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);		// Clear Screen And Depth Buffer

	//eyeDrawSet();
	glEnable(GL_TEXTURE_2D);
	glBindTexture(GL_TEXTURE_2D, AppGloble::textureNUM);     // Bind To The Texture ID
	glUseProgram(AppGloble::prgrm);
	//glUseProgram(0);

	//glutSolidTeapot(1.0);

	//*
	glBegin(GL_QUADS);

	glColor3f(1.0f,1.0f,1.0f);
	{
		//glTexCoord2f(0.0f, 0.0f);
		glVertex3f(-0.0f, -2.0f, -2.0f);

		//glTexCoord2f(1.0f, 0.0f);
		glVertex3f(0.0f, 2.0f, -2.0f);

		//glTexCoord2f(1.0f, 1.0f);
		glVertex3f(0.0f, 2.0f, 2.0f);

		glVertex3f(0.0f, -2.0f, 2.0f);
	}

	glEnd();
	//*/

	glUseProgram(0);
	glDisable(GL_TEXTURE_2D);
	drawCoordAxis();

	showScenInf();

	glutSwapBuffers();

	++AppGloble::frameRendered;

}

void drawModel_2pass()
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);		// Clear Screen And Depth Buffer

	//eyeDrawSet();
	glEnable(GL_TEXTURE_2D);
	glBindTexture(GL_TEXTURE_2D, AppGloble::textureNUM);     // Bind To The Texture ID
	glUseProgram(AppGloble::prgrm);
	//glUseProgram(0);

	glutSolidTeapot(1.0);

	//*
	glBegin(GL_QUADS);

	glColor3f(1.0f,1.0f,1.0f);
	{
	//glTexCoord2f(0.0f, 0.0f);
	glVertex3f(-0.0f, -1.0f, -1.0f);

	//glTexCoord2f(1.0f, 0.0f);
	glVertex3f(0.0f, 1.0f, -1.0f);

	//glTexCoord2f(1.0f, 1.0f);
	glVertex3f(0.0f, 1.0f, 1.0f);

	glVertex3f(0.0f, -1.0f, 1.0f);
	}

	glEnd();
	//*/

	glReadBuffer(GL_BACK_LEFT);
	glBindTexture(GL_TEXTURE_2D, AppGloble::tmpTextureNUM);     // Bind To The Texture ID
	glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 300, 200, 200, 200, 0);

	glUseProgram(0);

	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);		// Clear Screen And Depth Buffer

	//glEnable(GL_TEXTURE_2D);
	//glBindTexture(GL_TEXTURE_2D, AppGloble::tmpTextureNUM);     // Bind To The Texture ID

	GLfloat zZeroPlane[] = {0.0f, 0.0f, 1.0f, 0.0f};
	GLfloat xZeroPlane[] = {1.0f, 0.0f, 0.0f, 0.0f};

	glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
	glTexGenfv(GL_S, GL_OBJECT_PLANE, xZeroPlane);
	glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
	glTexGenfv(GL_T, GL_OBJECT_PLANE, zZeroPlane);	

	glEnable(GL_TEXTURE_GEN_S);
	glEnable(GL_TEXTURE_GEN_T);

	glutSolidTeapot(1.0);

	glUseProgram(0);
	glDisable(GL_TEXTURE_2D);
	drawCoordAxis();

	showScenInf();

	glutSwapBuffers();

	++AppGloble::frameRendered;
}


GLint AppGloble::MST_zoomLOC = 0;
GLint AppGloble::MST_xcenLOC = 0;
GLint AppGloble::MST_ycenLOC = 0;
GLint AppGloble::MST_inixLOC = 0;
GLint AppGloble::MST_iniyLOC = 0;
GLint AppGloble::MST_maxforNumLOC = 0;

float AppGloble::MST_zoom = 1.0f;
float AppGloble::MST_xcen = 0.0f;
float AppGloble::MST_ycen = 0.0f;
float AppGloble::MST_inix = -0.765f;
float AppGloble::MST_iniy = 0.11f;
float AppGloble::MST_maxforNum = 20.0f;

//于10月9日上午11点15分跑通第一个GLSL程序 cool，AZA AZA，go ahead
int demo_Mandelbrot()
{
	int argc=0;
	char** argv=NULL;
	glutInit(&argc, argv);
	//创建窗口
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);	

	glutInitWindowSize(800,600);

	glutInitWindowPosition(100,100);

	glutCreateWindow("Mandelbrot GLSL");	

	//设置opengl状态机
	//glClearColor(0.2, 0.4, 0.6, 1.0);	
	glClearColor(0.0, 0.0, 0.0, 1.0);	

	glDepthFunc(GL_LESS);
	glEnable(GL_DEPTH_TEST);

	glCullFace(GL_FRONT_AND_BACK);
	glShadeModel(GL_SMOOTH);

	//AppGloble::noweye.eyez=	1.0f;
	//AppGloble::noweye.eyey=1.0f;
	//AppGloble::noweye.eyex=1.0f;

	AppGloble::noweye.radius=2.0;
	AppGloble::noweye.longitude=0.0f;
	AppGloble::noweye.latitude = 0.0f;

	AppGloble::noweye.computPosXYZ();

	glMatrixMode(GL_MODELVIEW);

	glLoadIdentity();	

	gluLookAt(AppGloble::noweye.eyex,	
			  AppGloble::noweye.eyey,
			  AppGloble::noweye.eyez,
			  0.0,	0.0,	0.0,
			  0.0,	0.0,	1.0);

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(60.0, 1.0, 0.1, 1000.0);	//第三个参数,近裁减面,不能为0

	//lightAndMatSet();

	//初始化GLSL环境
	//*
	glewInit();
	if (glewIsSupported("GL_VERSION_2_0"))
	{
		std::cout<<"Ready for OpenGL 2.0"<<std::endl;

		//texSet();
	
		if(shaderSetMandelbrotSet())
		{
			glutDisplayFunc(drawModel_teapot);

			glutIdleFunc(drawModel_teapot);

			glutMouseFunc(mouseFunc);

			glutMotionFunc(mouseMotionFunc);

			glutKeyboardFunc(keyFunc_mandelbort);

			glutTimerFunc(500, computFrameRate, 0);

			glutMainLoop();	
		}
	}	
	else 
	{
		std::cout<<"penGL 2.0 not supported"<<std::endl;		
	}

	int pp;
	cin>>pp;

	return 0;
}

//于10月9日上午11点15分跑通第一个GLSL程序 cool，AZA AZA，go ahead
int demo_twoPass()
{
	int argc=0;
	char** argv=NULL;
	glutInit(&argc, argv);
	//创建窗口
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);	

	glutInitWindowSize(800,600);

	glutInitWindowPosition(100,100);

	glutCreateWindow("Terrian Zjn PBR GLSL");	

	//设置opengl状态机
	//glClearColor(0.2, 0.4, 0.6, 1.0);	
	glClearColor(0.0, 0.0, 0.0, 1.0);	

	glDepthFunc(GL_LESS);
	glEnable(GL_DEPTH_TEST);

	glCullFace(GL_FRONT_AND_BACK);
	glShadeModel(GL_SMOOTH);

	//AppGloble::noweye.eyez=	AppGloble::noweye.eyey=0.0f;
	//AppGloble::noweye.eyex=9.0f;

	//AppGloble::noweye.radius=9.0;
	//AppGloble::noweye.longitude=0.0f;
	//AppGloble::noweye.latitude = 0.0f;

	//eyeDrawSet();

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(60.0, 1.0, 0.1, 1000.0);	//第三个参数,近裁减面,不能为0

	//lightAndMatSet();

	//初始化GLSL环境
	//*
	glewInit();
	if (glewIsSupported("GL_VERSION_2_0"))
	{
		std::cout<<std::endl<<"Ready for OpenGL 2.0"<<std::endl;

		texSet();

		if(shaderSet())
		//if(shaderSetMandelbrotSet())
		{
			glutDisplayFunc(drawModel_2pass);//drawModel_teapot

			glutIdleFunc(drawModel_2pass);//

			glutMouseFunc(mouseFunc);

			glutMotionFunc(mouseMotionFunc);

			glutKeyboardFunc(keyFunc);

			glutTimerFunc(500, computFrameRate, 0);

			glutMainLoop();	
		}
	}	
	else 
	{
		std::cout<<"penGL 2.0 not supported"<<std::endl;		
	}

	int pp;
	cin>>pp;

	return 0;
}
